package cool.taomu.toolkit.redisson.provider

import com.google.inject.Inject
import com.google.inject.Provider
import com.google.inject.name.Named
import cool.taomu.toolkit.TaomuConfigure
import java.util.Map
import org.redisson.Redisson
import org.redisson.api.RedissonClient
import org.redisson.config.Config

class RedissonProvider implements Provider<RedissonClient> {
    TaomuConfigure config

    @Inject
    @Named("taomu.redisson")
    String name;

    new(TaomuConfigure config) {
        this.config = config
    }

    override get() {
        var Config config = new Config();
        val server = config.useSingleServer();
        this.config.binder("taomu.redis", [
            if (it instanceof Map) {
                if (it.get("name").equals(name)) {
                    server.address = #[
                        "redis://"+
                        it.get("host"),
                        it.get("port")
                    ].join(":")
                    server.password = it.get("password") as String
                }
            }
        ])
        return Redisson.create(config);
    }

}
